Skip to content

Conversation

@sabapathy7
Copy link
Owner

📋 Summary
This PR migrates NetworkKit to Swift 6.2 with Approachable Concurrency support, enabling strict concurrency checking while maintaining full backward compatibility with all existing API patterns (async/await, Combine, closures).
🎯 Motivation
Adopt Swift 6.2's "Approachable Concurrency" features (SE-0461)
Enable strict concurrency checking for data-race safety
Demonstrate modern Swift concurrency patterns
Prepare codebase for conference talk on Swift 6.2 concurrency
🔧 Changes Made

  1. Package Configuration
    Updated swift-tools-version from 5.8 to 6.0
    Enabled StrictConcurrency feature flag
    Enabled ApproachableConcurrency feature flag (Swift 6.2)
    Enabled InternalImportsByDefault experimental feature
    Applied concurrency features to both main and test targets
  2. Core Type Sendable Conformance
    EndPoint: Added Sendable conformance with documentation
    NetworkError: Added Sendable conformance, fixed default → explicit case .unknown
    RequestMethod: Added Sendable conformance, standardized PATCH → patch naming
  3. Protocol Updates (Networkable)
    Added Sendable protocol conformance
    Added Sendable constraints to all generic type parameters (T: Decodable & Sendable)
    Enhanced documentation with SE-0461 isolation context notes
    Added sendRequestWithContinuation method to demonstrate bridging pattern
    Organized methods with clear MARK comments by pattern type
  4. NetworkService Refactoring
    Thread-Safety Improvements
    Added @unchecked Sendable conformance (manually verified safe)
    Replaced URLSession.shared with immutable instance property
    Added dependency injection via init(configuration:)
    Added convenience init() for default configuration
    Error Handling Improvements
    Replaced try? with proper do-catch blocks for better error propagation
    Fixed preconditionFailure in Combine method → returns Fail publisher
    Improved variable naming (avoided shadowing: response → httpResponse)
    Corrected error types (.invalidURL → .unexpectedStatusCode where appropriate)
    Code Organization
    Converted extension methods to private methods
    Changed fileprivate → private for better encapsulation
    Added comprehensive method documentation
    Added MARK comments for code navigation

With InternalImportsByDefault experimental feature enabled in Swift 6.2,
imports are treated as internal by default. This caused compilation errors
when public API methods used URLSessionConfiguration and AnyPublisher types.

Changed to @_exported import to ensure these types remain accessible to
consumers of the NetworkKit public API.
Added Sendable conformance to all data model types for Swift 6 concurrency:

- EndPoint protocol: Sendable conformance with documentation
- NetworkError enum: Sendable conformance with documentation
- RequestMethod enum: Sendable conformance with documentation

Additional improvements:
- NetworkError: Changed 'default' to explicit 'case .unknown' (exhaustive)
- RequestMethod: Standardized 'PATCH' to lowercase 'patch' (Swift naming)

These changes ensure all data types can be safely shared across
concurrency domains without data races.

Related: Swift 6 strict concurrency checking
Refactored NetworkService for Swift 6 concurrency safety:

- Added @unchecked Sendable conformance (verified thread-safe)
- Replaced URLSession.shared with immutable instance property
- Added init(configuration:) and convenience init()
- Enables dependency injection for testing

Benefits:
- No shared global state
- Thread-safe by design (immutable properties)
- Testable via URLSessionConfiguration injection
- Proper initialization patterns

Related: Swift 6 strict concurrency, dependency injection
Multiple improvements to NetworkService implementation:

Error Handling:
- Replaced try? with proper do-catch blocks
- Changed preconditionFailure to Fail publisher (Combine)
- Fixed incorrect error types (.invalidURL -> .unexpectedStatusCode)
- Better variable naming (response -> httpResponse)

Code Organization:
- Converted extension to private methods
- Added comprehensive documentation
- Added MARK comments for navigation
- Removed empty public init()

These changes improve maintainability and prevent runtime crashes.

Breaking: Combine method returns Fail instead of crashing.
@sabapathy7 sabapathy7 self-assigned this Oct 29, 2025
@sabapathy7 sabapathy7 added enhancement New feature or request swift 6.2 labels Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request swift 6.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants